/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 */
package com.jeesite.modules.vehicle.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.idgen.IdGen;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.general.utils.CosApiUtils;
import com.jeesite.modules.sys.entity.DictData;
import com.jeesite.modules.sys.utils.DictUtils;
import com.jeesite.modules.vehicle.entity.CarApi;
import com.jeesite.modules.vehicle.service.CarApiService;
import org.springframework.beans.factory.annotation.  Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试数据Controller
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/vehicle/")
public class CarApiController extends BaseController {

	@Autowired
	private CarApiService carApiService;

	DecimalFormat df = new DecimalFormat("0.00");

	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "findCarList")
	@ResponseBody
	public Page<CarApi> findCarList(CarApi carApi, HttpServletRequest request, HttpServletResponse response) {
		carApi.setPage(new Page<>(request, response));
		Page<CarApi> page = carApiService.findPage(carApi);
		List<CarApi> list = page.getList();
		for(int i=0;i<list.size();i++){
			list.get(i).setShowSellPrice(df.format(list.get(i).getSellPrice()/100));
			list.get(i).setCoverUrl(CosApiUtils.URL+list.get(i).getCoverUrl());
		}
		return page;
	}

	/**
	 * 查询车分类
	 * */
	@RequestMapping(value = "findCarType")
	@ResponseBody
	public String findCarType(CarApi carApi){
		return renderResult(Global.TRUE, text("获取成功！"), carApiService.findCarType(carApi));
	}

	/**
	 * 查询车分类并进行字母排序
	 * */
	@RequestMapping(value = "findCarLetterType")
	@ResponseBody
	public String findCarLetterType(CarApi carApi){
		Map map = new HashMap();
		Map<String, List<CarApi>> list = carApiService.findCarLetterType(carApi);
		map.put("list", list);
		map.put("show", list.size()>0?true:false);
		return renderResult(Global.TRUE, text("获取成功！"), map);
	}

	/**
	 * 查询车详情
	 * @param carId
	 * */
	@RequestMapping(value = "getCarDetail")
	@ResponseBody
	public String getCarDetail(String carId){
		return renderResult(Global.TRUE, text("获取成功！"), carApiService.getCarDetail(carId));
	}

	/**
	 * 查询车品牌
	 * */
	@RequestMapping(value = "findCarBrand")
	@ResponseBody
	public String findCarBrand(Integer pageSize){
		return renderResult(Global.TRUE, text("获取成功！"), carApiService.findCarBrand(pageSize));
	}

	/**
	 * 查询车标签
	 * */
	@RequestMapping(value = "findDictList")
	@ResponseBody
	public String findDictList(){
		List<DictData> list = DictUtils.getDictList("h_car_label");
		JSONArray array = new JSONArray();
		for(int i=0;i<list.size();i++){
			array.add(list.get(i).getDictLabel());
		}
		return renderResult(Global.TRUE, text("获取成功！"), array);
	}

	/**
	 * 保存车信息
	 * */
	@RequestMapping(value = "saveCarInfo")
	@ResponseBody
	public JSONObject saveCarInfo(MultipartFile file, CarApi carApi){
		JSONObject res = new JSONObject();
		res.put("result","false");
		if(file != null){
			JSONObject files = CosApiUtils.uploadFile(file,"","car/"+carApi.getPrefix());
			if(files.getBoolean("result")){
				res.put("msg","图像上传错误，请稍后再试！");
				return res;
			}else {
				res.put("data",files.get("filePath"));
				if("c".equals(carApi.getImageType())){
					carApi.setCoverUrl(files.get("fileName2").toString());
					carApiService.save(carApi);
					res.put("foreignId", carApi.getCarId());
				}else{
					Map map = new HashMap();
					map.put("imageId", IdGen.nextId());
					map.put("foreignId", carApi.getForeignId());
					map.put("imageUrl", files.get("fileName2"));
					map.put("imageType", carApi.getImageType());
					map.put("createBy", carApi.getCreateBy());
					carApiService.insertImage(map);
				}
			}
		}
		res.put("result","true");
		res.put("msg","提交成功！");
		return res;
	}

	/**
	 * 下架车
	 * */
	@RequestMapping(value = "shelfCar")
	@ResponseBody
	public String shelfCar(CarApi carApi){
		carApiService.delete(carApi);
		return renderResult(Global.TRUE, text("下架成功！"));
	}

	/**
	 * 查询车详情（移动端查看）
	 * @param carId
	 * */
	@RequestMapping(value = "getCarMobileDetail")
	@ResponseBody
	public String getCarMobileDetail(String carId){
		return renderResult(Global.TRUE, text("获取成功！"), carApiService.getCarMobileDetail(carId));
	}

	/**
	 * 删除图片
	 * @param carApi
	 * */
	@RequestMapping(value = "deleteImage")
	@ResponseBody
	public String deleteImage(CarApi carApi){
		carApiService.deleteImage(carApi);
		CosApiUtils.delCOSFile(carApi.getImageUrl(),carApi.getPrefix());
		return renderResult(Global.TRUE, text("获取成功！"));
	}
}